<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2016/6/7
  Time: 7:47
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="/wechat/ucenter/recharge.css">
    <jsp:include page="../public/head.jsp" flush="true" />

</head>
<body>
<header >
    <div class="h-cont">
        <div class="h-l"><i class="iconfont icon-person" onclick="history.go(-1)">&#xe625;</i></div>
        <div class="h-r"><span>${title}</span></div>
        <div class="h-t"><a href="/ucenter/rechargeRecord.do"><span>记录</span></a></div>
    </div>
</header>
<div class="h-ocupie"></div>
<section>
    <div class="my_recharge">
        <div class="tip">
           <a><div class="tip3 current">普通充值</div></a>
            <a  onclick="return window.location.replace('/ucenter/cardRecharge.do')"><div class="tip4 ">充值卡充值</div></a>
        </div>
        <form>
            <div class="cont">
                <span>账户余额:</span>
                <div class="txt t1">￥${userinfo.money}元</div>
            </div>
            <div class="tittle">请输入金额:</div>
            <div class="cont c1">
                <span><i class="iconfont icon-cash">&#xe623;</i>充值金额:</span>
                <input type="number" class="txt" name="" id="money" placeholder="要大于5元哦`(*∩_∩*)′" onkeyup="value=value.replace(/[^\d.]/g,'')">
            </div>
            <div class="paybody">
                <div class="ptit">请选择下列的付款方式:</div>
                <div class="pcont last">
                    <input type="radio" class="myweixin" name="paytype" value="3"><i class="iconfont icon2">&#xe628;</i>
                    <i class="iconfont icon1 weixin">&#xe61a;</i><span>微信支付</span>
                </div>
            </div>
            <input type="button" name="" value="立即充值" class="recharge_btn" onclick="dopay()" id="recharge_btn">
        </form>
    </div>
</section>
<footer>
    <div  class="my_ocupie"></div>
</footer>
<script>

    var wxappId ;
    var wxtimeStamp ;
    var nonceStr ;
    var prepay_id ;
    var wxpaySign ;

    function dopay() {
        var html = "<div><div>";
        $("#id").append(html);
        var witch = $("input[name='paytype']:checked").val();
        var money = $("#money").val();

        if (money.length == 0){
//            tip("","还未输入金额",true);
            alert("还未输入金额");
            return;
        }

        if (witch == null){
//            tip("","还未选择支付方式",true);
            alert("还未选择支付方式");
            return;
        }

        if (!confirm("你确定要在${rechargeCityName}充值吗？不确定的话就到首页切换一下城市吧！")){
            return;
        }

        $("#recharge_btn").attr("disabled","true");
        $.post("/ucenter/makeRecharge.do",{money:money,cityId:${rechargeCityId}},function (data) {
            console.log(data);
            if (!data.data.success){
//                tip("",data.msg,true);
                alert(data.data.msg);
                $("#recharge_btn").removeAttr("disabled");
            }else {
                console.log(data.data.data);
                wxappId = data.data.data.weiPay.appId;
                wxtimeStamp = data.data.data.weiPay.timeStamp;
                nonceStr = data.data.data.weiPay.nonceStr;
                prepay_id = data.data.data.weiPay.prepayId;
                wxpaySign = data.data.data.weiPay.paySign;

                switch (witch){
                    case '1':
                        ye_pay();
                        break;
                    case '2':
                        alert("支付宝支付");
                        break;
                    case '3':
                        wechat_pay();
                        break;
                }
            }
        },"json");

    }

    function wechat_pay() {
        if (typeof WeixinJSBridge == "undefined"){
            if( document.addEventListener ){
                document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
            }else if (document.attachEvent){
                document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
            }
        }else{
            onBridgeReady();
        }
    }
    function onBridgeReady(){

        WeixinJSBridge.invoke(
                'getBrandWCPayRequest', {
                    "appId": wxappId,     //公众号名称，由商户传入
                    "timeStamp":wxtimeStamp,         //时间戳，自1970年以来的秒数
                    "nonceStr": nonceStr, //随机串
                    "package": "prepay_id="+prepay_id,
                    "signType": "MD5",         //微信签名方式：
                    "paySign": wxpaySign //微信签名
                },
                function(res){
                    if(res.err_msg == "get_brand_wcpay_request:ok" ) {
                        window.location.replace("/ucenter/rechargereturn.do");
                    }else {
                        $("#recharge_btn").removeAttrs("disabled");
                    }    // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。
                }
        );
    }
</script>
</body>
</html>
